#include "../include/StringOprate.h"
#include <string.h>

//字符串转成16进制高低位数据
void hex_asc(const unsigned char *strSrc, unsigned char *strDes,
		int len)
{
	int i;
	for (i = 0; i < len; i++)
	{
		if ((strDes[2 * i] = (strSrc[i] >> 4)) < 10)
			strDes[2 * i] += '0';
		else
			strDes[2 * i] += ('A' - 0x0a);
		if ((strDes[2 * i + 1] = (strSrc[i] & 0x0f)) < 10)
			strDes[2 * i + 1] += '0';
		else
			strDes[2 * i + 1] += ('A' - 0x0a);
	}
}

void hex_asc_char(unsigned char chSrc, unsigned char *strDes)
{
	if ((strDes[0] = (chSrc >> 4)) < 10) //高位
		strDes[0] += '0';
	else
		strDes[0] += ('A' - 0x0a);
	if ((strDes[1] = (chSrc & 0x0f)) < 10) //低位
		strDes[1] += '0';
	else
		strDes[1] += ('A' - 0x0a);
}

//字符类型转换
//convert char data format to unsigned char data format
int Str_Array_Signal(char *str1, unsigned char *str2)
{
	unsigned int i;
	unsigned int length;
	length = strlen(str1);
	for (i = 0; i < length; i++)
	{
		str2[i] = str1[i];
	}
	return i;
}

//字符类型转换
//convert unsigned char data format to char data format
int Array_String(unsigned char *str1, char *str2)
{
	unsigned int i;
	unsigned int length = 10;
	for (i = 0; i < length; i++)
	{
		str2[i] = str1[i];
	}
	return i;
}

//16进制格式数据串变为ASCII码格式
int HexToStr(unsigned char *str1, unsigned char *str2)
{
	int i;
	unsigned char H_BYTE; //高位
	unsigned char L_BYTE; //低位
	int length;
	int k;
	int j;
	char str[500] = "";
	;
	length = strlen((char *) str1); //获取输入字符串的长度
	k = 0;
	for (i = 0; i < length; i++)
	{
		if (str1[i] != ' ')
		{
			str[k] = str1[i];
			k++;
		}
	}
	length = strlen(str);
	length = length / 2;
	k = 0;
	j = 0;
	for (i = 0; i < length; i++)
	{
		H_BYTE = str[j];
		j++;
		L_BYTE = str[j];
		j++;

		if ((H_BYTE >= 0x30) && (H_BYTE <= 0x39))
		{
			H_BYTE = (H_BYTE << 4) & 0xf0;
			;
		}
		else if ((H_BYTE >= 'A') && (H_BYTE <= 'F'))
		{
			H_BYTE = ((H_BYTE - 0x37) << 4) & 0xf0;
		}
		else if ((H_BYTE >= 'a') && (H_BYTE <= 'f'))
		{
			H_BYTE = ((H_BYTE - 0x57) << 4) & 0xf0;
		}
		else
			H_BYTE = 0;

		if ((L_BYTE >= 0x30) && (L_BYTE <= 0x39))
		{
			L_BYTE = L_BYTE & 0x0F;
			;
		}
		else if ((L_BYTE >= 'A') && (L_BYTE <= 'F'))
		{
			L_BYTE = (L_BYTE - 0x37) & 0x0F;
		}
		else if ((L_BYTE >= 'a') && (L_BYTE <= 'f'))
		{
			L_BYTE = (L_BYTE - 0x57) & 0x0F;
		}
		else
			L_BYTE = 0;
		str2[k] = H_BYTE + L_BYTE;
		k++;
	}
	return k;
}

//去掉字符串中的空格
void chomp(char*str)
{
	int i = 0;
	int j = 0;
	while ((*(str + i) = *(str + j++)) != '\0')
	{
		if (*(str + i) != ' ')
		{
			i++;
		}
	}
}
